/*
 *	This class mostly does opening and saving.  Plus setting the matrices
 *	and ScrollView up for max class size (45)
 *	Rob Ferrante, 2/92
 */
#import "Manager.h"
#import <appkit/Window.h>
#import <appkit/ScrollView.h>
#import <appkit/Matrix.h>
#import <appkit/TextField.h>
#import <appkit/OpenPanel.h>
#import <streams/streams.h>
#import <appkit/nextstd.h>
#import "strings.h"

#define		FILESTAMP		"***   Produced By CurveGrader   ***"
@implementation Manager

char  *titleFromString(char *str)
{
	static char  title[36];
	int len = strlen(str);
	
	strcpy(title,"...");
	if (len >32) strcpy(&title[3],&str[len -32]);
	else strcpy(&title[3],str);
	
	return title;
}

- init
{
	[super init];
	currentPathName = NXCopyStringBuffer("~/UNTITLED");
	return self;
}

- saveToFileWithPanel:sender
{
	id  saver = [SavePanel new];
	
	if([saver runModal]==1) {
		free( currentPathName);
		currentPathName = NXCopyStringBuffer([saver filename]);
		[quizWindow setTitle:titleFromString(currentPathName)];
		[self saveToCurrentFile:self];
	}
	
    return self;
}

- readFromCurrentFile:sender
{
	NXStream *ms;
	int i, rows, cols;
	char  name[31],grade[5];
	float score;
	
	if((ms =NXMapFile(currentPathName, NX_READONLY))==NULL) {
		NXRunAlertPanel("Alert", "Couldn't read file", "OK", NULL, NULL);
		return self;
	}
	

	[names getNumRows:&rows numCols:&cols];
	name[0]='\0';
	NXScanf(ms, "\"%[^\"]\"\n", name);
	if (strcmp(name, FILESTAMP) != 0) {
		NXRunAlertPanel("Alert", "Not a CurveGrader File", "OK", NULL, NULL);
		return self;
	}
	name[0]='\0';
	NXScanf(ms, "\"%[^\"]\"\n", name);
	[classField setStringValue:name];
	name[0]='\0';
	NXScanf(ms, "\"%[^\"]\"\n", name);
	[quizField setStringValue:name];
	
	for (i=0; i<rows; i++) {
		name[0]='\0'; grade[0]='\0';score=0;
		NXScanf(ms, "\"%[^\"]\",%f,\"%[^\"]\"\n", name, &score, grade);
		[[names cellAt:i :0] setStringValue:name];
		[[scores cellAt:i :0] setFloatValue:score];
		[[grades cellAt:i :0] setStringValue:grade];
	}
	
	NXCloseMemory(ms, NX_FREEBUFFER);
	[quizWindow setTitle:titleFromString(currentPathName)];
	
    return self;
}

- saveToCurrentFile:sender
{
	NXStream *ms;
	int i, rows, cols;
	
	ms = NXMapFile(currentPathName, NX_WRITEONLY);	
	if (ms == NULL) ms = NXOpenMemory(NULL, 0, NX_WRITEONLY);

	[names getNumRows:&rows numCols:&cols];
	
	NXPrintf(ms, "\"%s\"\n", FILESTAMP);
	NXPrintf(ms, "\"%s\"\n", [classField stringValue]);
	NXPrintf(ms, "\"%s\"\n", [quizField stringValue]);
	for (i=0; i<rows; i++)
		NXPrintf(ms, "\"%s\",%f,\"%s\"\n",[[names cellAt:i :0] stringValue],
		                                  [[scores cellAt:i :0] floatValue],
										  [[grades cellAt:i :0] stringValue]);
    if ( NXSaveToFile( ms, currentPathName)== -1)
		NXRunAlertPanel("Alert", "Couldn't write file", "OK", NULL, NULL);
	
	NXCloseMemory(ms, NX_FREEBUFFER);
	
	return self;
}

- readFromFileWithPanel:sender
{
	id  opener = [OpenPanel new];
	
	if([opener runModal]==1) {
		free( currentPathName);
		currentPathName = NXCopyStringBuffer([opener filename]);
		[self readFromCurrentFile:self];
	}
	
    return self;
}

- appDidInit:sender
{
	NXRect  matrixRect, docRect, clipRect;
	[names notifyAncestorWhenFrameChanged:YES];
	[grades renewRows:45 cols:1];
	[grades sizeToCells];
	[scores renewRows:45 cols:1];
	[scores sizeToCells];
	[names renewRows:45 cols:1];
	[names sizeToCells];
	[names getBounds:&matrixRect];
	[[scroller docView] getBounds:&docRect];
	[[[scroller docView] superview] getBounds:&clipRect];
	[[scroller docView] sizeTo:docRect.size.width :matrixRect.size.height+20];
	[[scroller docView] moveTo:0 :(clipRect.size.height -docRect.size.height)];
	[scroller reflectScroll:[[scroller docView] superview]];
	[scroller display];
	return self;
}
	
@end
